package com.mmall.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @author:lzq
 * @date: 2018/3/20 11:59
 */
public class PropertiesUtil {

    private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    private static Properties properties;

    //静态代码块，类在被加载的时候就会被加载 静态代码块>普通代码块>构造函数
    static {
        String fileName="mmall.properties";
        properties=new Properties();
        try {
            properties.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"utf-8"));
        } catch (IOException e) {
            logger.error("读取配置文件异常",e);
        }
    }

    public static String getProperty(String key){
        if(StringUtils.isEmpty(key)){
            return "";
        }
        return properties.getProperty(key.trim()).trim();
    }

    public static String getProperty(String key,String defaultValue){
        if(StringUtils.isEmpty(key)){
            return defaultValue;
        }
        return properties.getProperty(key.trim()).trim();
    }
}
